package com.ruoyi.durian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.DurianConstant;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DurianRedisKeysType;
import com.ruoyi.common.enums.OrderType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.durian.domain.LlInvoiceHistory;
import com.ruoyi.durian.domain.LlOrder;
import com.ruoyi.durian.domain.LlOrderInfo;
import com.ruoyi.durian.mapper.LlInvoiceHistoryMapper;
import com.ruoyi.durian.service.LlInvoiceHistoryService;
import com.ruoyi.durian.service.LlOrderInfoService;
import com.ruoyi.durian.service.LlOrderService;
import com.ruoyi.durian.utils.SnowflakeUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author Libra
 * @description 针对表【ll_invoice_history(订单模块-开票历史记录表)】的数据库操作Service实现
 * @createDate 2024-08-06 14:22:51
 */
@Service
public class LlInvoiceHistoryServiceImpl extends ServiceImpl<LlInvoiceHistoryMapper, LlInvoiceHistory>
        implements LlInvoiceHistoryService {


    @Resource
    LlOrderInfoService llOrderInfoService;

    @Resource
    SysUserMapper sysUserMapper;

    /**
     * 历史开票记录
     *
     * @param
     * @return
     */
    @Override
    public List<LlInvoiceHistory> historyList() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<LlInvoiceHistory> eq = new LambdaQueryWrapper<LlInvoiceHistory>()
                .eq(LlInvoiceHistory::getUserId, userId)
                .eq(LlInvoiceHistory::getIsDelete, 0);
        List<LlInvoiceHistory> list = this.list(eq);
        return list;
    }

    /**
     * 保存开票记录
     *
     * @param llInvoiceHistory
     * @return
     */
    @Override
    @Transactional
    public AjaxResult saveInvoice(LlInvoiceHistory llInvoiceHistory) {
        if (llInvoiceHistory == null) {
            return AjaxResult.error("参数错误");
        }
        //雪花算法
        long nextId = new SnowflakeUtils(1, 1).nextId();
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        String[] parts = llInvoiceHistory.getOrderId().split(","); // 使用逗号作为分隔符将字符串分割成数组
        Arrays.stream(parts).forEach(item -> {
            LambdaUpdateWrapper<LlOrderInfo> set = new LambdaUpdateWrapper<LlOrderInfo>()
                    .eq(LlOrderInfo::getOrderId, item)
                    .set(LlOrderInfo::getInvoiceStatus, 0);
            llOrderInfoService.update(set);
        });
        llInvoiceHistory.setFirmCode(String.valueOf(nextId));
        llInvoiceHistory.setOrderId(llInvoiceHistory.getOrderId());
        llInvoiceHistory.setPhone(sysUser.getPhonenumber());
        llInvoiceHistory.setUserId(userId);
        llInvoiceHistory.setFirmName(DurianConstant.FIRM_NAME);
        llInvoiceHistory.setStartTime(new Date());
        llInvoiceHistory.setCreateTime(new Date());
        save(llInvoiceHistory);
        return AjaxResult.success(llInvoiceHistory);
    }
    public static void main(String[] args) {
        long nextId = new SnowflakeUtils(1, 1).nextId();
        System.out.println(nextId);
    }
}




